<?php
	header('Content-type:text/html;charset=utf-8');
	class Shop_goodsModel extends Model{

		public $table = 'shop_goods';

		public $validate = array(

			// array('gname','nonull','商品名称不能为空',2,3),
			// array('gnumber','nonull','商品编号不能为空',2,3),
			// array('unit','nonull','商品单位不能为空',2,3),
			// array('shopprice','nonull','商城价格不能为空',2,3),
			// array('markprice','nonull','市场价格不能为空',2,3)

			);

		 public $auto = array(
		 	array('uptime','time','function',2,3),
		 	array('downtime','_downtime','method',2,3),
		 	array('pic2','_pic2','method',2,3),
		 	);

		 public function _downtime($downtime){
		 	if (isset($downtime)) {

		 		return $downtime = time() + 3600*$downtime;
		 	}
		 	
		 }

		 public function _pic2($pic2){

		 	$pic2 = trim($_POST['pic2'],',');

		 	return $pic2;
		 }

		 public function thumb($pic){
		 	$img = new Image();
		 	$picData = explode(',', $pic);
		 	$small = '';
		 	$middle = '';
		 	$big = '';

			foreach ($picData as $k => $v) {
				//如果有图片上传地址就上传
				if($v){
					//将上传的图片缩略成60*60
					$thumbPath = $img->thumb($v,'',60,60);
					//将小图的地址用','号连接
					$small .= $thumbPath . ",";
					//替换小图片路径$thumbPath的'_thumb'为'_600*600_thumb'的名字
					//如果不替换名字下面的图片会覆盖上面的图片,地址也就都一样了
					$middleName = str_replace('_thumb', '_600x600_thumb', $thumbPath);
					$middle .= $img->thumb($v,$middleName,600,600) . ",";
					$bigName = str_replace('_thumb', '_1000x1000_thumb', $thumbPath);
					$big .= $img->thumb($v,$bigName,1000,1000) . ",";
				}
				
				
			}
		// 	// p($small);echo '<hr/>';
		// 	// p($middle);echo '<hr/>';
		// 	// p($big);echo '<hr/>';
			//将连接到的小图，中图，大图的地址去掉两边的逗号
			$small = trim($small,',');
			$middle = trim($middle,',');
			$big = trim($big,',');
			// p($middle);
			$tempt=array(
				'small'=>$small,
				'middle'=>$middle,
				'big'=>$big,

				);
			return $tempt;

		}

		public function add_data(){
			if (!$this->create()) {
				return false;
			}
			$aid =$_SESSION['aid'];
			//当应用$this->create()时候，就生成了$this->data,
			$this->data['shop_admin_aid']=$aid;
			$data = array(
				'shop_admin_aid'=>$aid,
				);

			// p(Q('post.'));die;
			//auto方法和$data = array();两种方法都是向数据库中插入数据，两种方法是相互抵触的，用了auto
			//就没法用$data = array();了.
			$gid = $this->add();
			// //因为$_POST是全局数组，所以在页面中的哪个位置都可以获得他的值
			$pic = trim($_POST['pic1'],',');
			//上传图片
			$thumb=$this->thumb($pic);
			//商品详情和售后服务的上传
			//百度编辑器上传的时候用$_POST
			//如果用Q('post'),就会实体化
			//出现标签一样的东西
			$infor = $_POST['infor'];
			$service = $_POST['service'];

			$detailData = array(
				'shop_goods_gid'=>$gid,
				'infor'         =>$infor,
				'service'		=>$service,
				'small'			=>$thumb['small'],
				'middle'		=>$thumb['middle'],
				'big'			=>$thumb['big'],
				);
			K('Shop_detail')->add($detailData);
			
			$added = Q('post.added');
			$gtvalue = Q('post.gtvalue');
			$taid = Q('post.taid');

			$tt=array_map(null, $taid,$gtvalue,$added);
			// p($tt);die;

			foreach ($tt as $u => $p) {
				$tr = array(
					'gtvalue' =>$p[1],
					'added' =>$p[2],
					'shop_type_attr_taid' =>$p[0],
					'shop_goods_gid'=>$gid,
				
					);
				K('Shop_goods_attr')->add($tr);
			}
			//要返回真，否则添加数据成功的提示无法显示
			return true;			
		}
	}










?>